// pages/modify/modify.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    details: '',
    date: '',
    show: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(JSON.parse(options.details))
    var date = JSON.parse(options.details).billDate.split("T")[0]

    this.setData({
      details: JSON.parse(options.details),
      date
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  formSubmit(e) {
    var billDate = this.data.date
    var billType = this.data.details.billType
    var money = e.detail.value.money
    var remarks = e.detail.value.remarks
    var type = this.data.details.type

    var userId = getApp().globalData.userId
    var bill = {
      billId: this.data.details.billId,
      userId: userId,
      billDate,
      billType,
      type,
      money,
      remarks
    }
    console.log(bill)
    Dialog.confirm({
      title: '是否确定修改',
      // message: '弹窗内容',
    })
      .then(() => {
        wx.request({
          url: 'http://127.0.0.1:8181/bill/modifyBill',
          data: bill,
          success(res) {
            console.log(res.data)
            wx.switchTab({
              url: '../../pages/index/index'
            })
          }
        })
      })
      .catch(() => {
        // on cancel
      });

  },

  onDisplay() {
    this.setData({
      show: true
    })
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
    console.log(this.data.date)
  },
})